﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.ViewModel;
using DataAccess.AccountManage;
using Microsoft.Reporting.WinForms;
using WinForm.Helper;

namespace WinForm.Reports
{
    public partial class ShowReportFrm : Form
    {
        /// <summary>
        ///     报表所在目录
        /// </summary>
        private static string _rdlcPath = "Rdlcs\\{0}";

        /// <summary>
        ///     报表开始时间
        /// </summary>
        private readonly DateTime _beginDate;

        /// <summary>
        ///     报表结束时间
        /// </summary>
        private readonly DateTime _endDate;

        /// <summary>
        ///     报表名称
        /// </summary>
        private readonly string _rdlcname;

        /// <summary>
        ///     标题
        /// </summary>
        private readonly string _title;

        public ShowReportFrm()
        {
            InitializeComponent();
        }

        public ShowReportFrm(string title, string rdlcname, DateTime begintime, DateTime endtime)
        {
            _title = title;
            _rdlcname = rdlcname;
            _beginDate = begintime;
            _endDate = endtime;
            InitializeComponent();
        }

        private void ShowReportFrm_Load(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                Text = _title;
                reportViewer1.ProcessingMode = ProcessingMode.Local;
                reportViewer1.LocalReport.ReportPath = string.Format(_rdlcPath, _rdlcname);


                //设置参数
                var cPerson =
                    new ReportParameter("CreateReportPerson",
                        SystemInfo.OperateName);
                var cTime =
                    new ReportParameter("CreateReportTime", DateTime.Now.ToString());
                var cAddress =
                    new ReportParameter("CreateReportAddress",
                        SystemInfo.OperateAddress);
                reportViewer1.LocalReport.SetParameters(new List<ReportParameter> {cPerson, cTime, cAddress});

                //获取数据源
                IBusinesses _b = new Businesses();
                ResultInfo<List<BusinessView>> businessvs =
                    _b.GetBusinessReport(_beginDate, _endDate, null, null, null);
                if (businessvs.ResultNumber == ResultEnum.Success)
                {
                    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", businessvs.Obj));
                }
                reportViewer1.RefreshReport();
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxInfo(errorMessage);
            }
        }
    }
}